#!/usr/bin/env python3
"""
简化的启动脚本
"""

import sys
import os

# 确保当前目录在Python路径中
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
    sys.path.insert(0, current_dir)

print("正在启动资产与IP综合管理系统...")

try:
    # 导入PyQt5
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QFont
    
    # 导入数据库模块
    from src.app.database import get_database_manager
    from src.app.database.init_data import initialize_all_data as init_data
    
    # 导入UI模块
    from src.app.ui.main_window import MainWindow
    
    # 创建应用程序
    app = QApplication(sys.argv)
    app.setApplicationName("资产与IP综合管理系统")
    app.setApplicationVersion("1.1")
    
    # 设置字体
    font = QFont("Microsoft YaHei", 9)
    app.setFont(font)
    
    # 设置高DPI支持
    app.setAttribute(Qt.AA_EnableHighDpiScaling, True)
    app.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
    
    # 初始化数据库
    print("正在初始化数据库...")
    db_manager = get_database_manager()
    
    if not db_manager.check_connection():
        print("数据库连接失败")
        sys.exit(1)
    
    db_manager.create_tables()
    
    with db_manager.get_session() as session:
        init_data(session)
    
    print("数据库初始化完成")
    
    # 创建并显示主窗口
    main_window = MainWindow()
    main_window.show()
    
    print("GUI界面启动成功")
    
    # 运行应用程序
    sys.exit(app.exec_())
    
except ImportError as e:
    print(f"导入错误: {e}")
    print("请确保已安装所有依赖包，运行: pip install -r requirements.txt")
    sys.exit(1)
    
except Exception as e:
    print(f"启动失败: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)